<template>
  <!-- 当 items 数组不为空时显示 foot 部分 -->
  <div v-if="items.length > 0" class="foot">
    <input type="checkbox" :checked="isAllChecked" @change="toggleAll" />
    <span>已办事项{{ completedCount }}/全部事项{{ totalCount }}</span>
    <button class="hasDo" @click="handleClearCompleted">清除已办</button>
    <button class="clearAll" @click="handleClearAll">清除所有</button>
  </div>
  <!-- 当 items 数组为空时显示 暂无待办事项 -->
  <h2 v-if="items.length <= 0">暂无待办事项</h2>
</template>

<script setup>
import { inject, computed } from 'vue';

// 注入父组件提供的数据和方法
const items = inject('items');
const completedCount = inject('completedCount');
const totalCount = inject('totalCount');
const handleClearCompleted = inject('handleClearCompleted');
const handleClearAll = inject('handleClearAll');

// 计算已完成的事项数
const isAllChecked = computed(() => items.value.length > 0 && items.value.every(item => item.checked));

// 切换所有项的选中状态
const toggleAll = () => {
  const newCheckedState = !isAllChecked.value;
  items.value.forEach(item => (item.checked = newCheckedState));
};
</script>

<style scoped>
.foot {
  position: relative;
  width: 100%;
  height: 40px;
  line-height: 40px;
  background: rgb(135, 206, 235);
  margin-top: 20px;
}

.foot input {
  position: absolute;
  left: 10px;
  top: 13px;
}

.foot span {
  display: inline-block;
  position: absolute;
  left: 40px;
}

.foot .hasDo {
  position: absolute;
  right: 90px;
  top: 10px;
  padding: 0 10px;
  border: 1px solid black;
  border-radius: 3px;
  cursor: pointer;
}

.foot .clearAll {
  position: absolute;
  right: 10px;
  top: 10px;
  padding: 0 10px;
  border: 1px solid black;
  border-radius: 3px;
  cursor: pointer;
}

h2 {
  text-align: center;
}
</style>
